/**
 * layui 正式 js
 * 尉强 2019-02-21
 *
 *
 *
**/
var son_index = null;
layui.define(['element','form','table','laydate','upload','carousel','flow'], function(exports) {
    var $				= layui.jquery,
		$parent			= parent.layui.jquery,
		upload			= layui.upload,
		carousel		= layui.carousel,
		element			= layui.element,
		layer			= layui.layer,
		parent_layer	= parent.layui.layer,
		top_layer		= parent.parent.layui.layer,
		parent_element	= parent.layui.element,
		table			= layui.table,
		form			= layui.form,
		flow			= layui.flow;
		laydate			= layui.laydate;
		
		
		
	flow.lazyimg(); 

	/**
	 * 自定义按钮
	 * .wy_but
	 *
	 * wy-event
	 * 		= lay_tab 打开窗口
	 * 		= lay_parent_tab 打开父级窗口
	 * 		= lay_ifram 打开ifram
	 * 		= lay_parent_ifram 打开父级ifram
	 * 		= lay_ajax_get 异步 get
	 * 		= lay_ajax_post 异步 post 批量操作
	 * 		= lay_ajax 异步刷新
	 *
	 * wy-url
	 *
	 *
	**/
	$(document).on("click",".wy_but",function(){
		var _this = $(this);
		var _event = _this.attr('wy-event');
		var WY_url = _this.attr('wy-url');
		if(!WY_url){
			layer.msg('请设置wy-url参数',{icon:0});
            return false;
		}
		if(_event == 'lay_tab'){
			var WY_id = _this.attr('wy-id');
			var WY_title = _this.attr('wy-title');
			fun_lay_tab(WY_url,WY_id,WY_title);
			return false;

		}else if(_event == 'lay_parent_tab'){
			var WY_id = _this.attr('wy-id');
			var WY_title = _this.attr('wy-title');
			fun_lay_parent_tab(WY_url,WY_id,WY_title);
			return false;
		}else if(_event == 'lay_parent_ifram'){
			var WY_id = _this.attr('wy-id');
			var WY_title = _this.attr('wy-title');
			fun_lay_parent_ifram(WY_url,WY_title);
			return false;
		}else if(_event == 'lay_top_ifram'){
			var WY_id = _this.attr('wy-id');
			var WY_title = _this.attr('wy-title');
			fun_lay_top_ifram(WY_url,WY_title);
			return false;
		}else if(_event == 'lay_ifram'){
			var WY_id = _this.attr('wy-id');
			var WY_title = _this.attr('wy-title');
			fun_lay_ifram(WY_url,WY_title);
			return false;
		}else if(_event == 'lay_ajax'){
			return false;

		}else if(_event == 'lay_ajax_post'){
			var WY_field = _this.attr('wy-field');
			var WY_table = _this.attr('wy-table');
			var WY_tips = _this.attr('wy-tips')||'';
			if(!WY_field){
				layer.msg('请设置wy-field',{icon: 2});
				return false;
			}
			if(!WY_table){
				layer.msg('请设置wy-table',{icon: 2});
				return false;
			}
			fun_lay_ajax_post(WY_url,WY_tips,WY_table,WY_field);
			return false;

		}else if(_event == 'lay_ajax_get'){
			var WY_tips = _this.attr('wy-tips')||'';
			var WY_table = _this.attr('wy-table')||'';
			var WY_refresh = _this.attr('wy-refresh')||'0';
			fun_lay_ajax_get(WY_url,WY_tips,WY_table,WY_refresh);
			return false;

		}
		return false;
	});
	// 异步 post 批量操作
	function fun_lay_ajax_post(WY_url,WY_tips,WY_table,WY_field){
		var checkStatus = table.checkStatus(WY_table);
		var check_data = checkStatus.data;
		if(check_data.length == 0){
			layer.msg('请选择需要操作的数据',{icon: 2});
			return false;
		}
		var field_data = [];
		$.each(check_data,function(m,n){
			field_data.push(n[WY_field])
		});
		if(field_data.length == 0){
			layer.msg('请选择需要操作的数据',{icon: 2});
			return false;
		}
		if(WY_tips){
			layer.confirm(WY_tips,{
				btn: ['确定','取消'] //按钮
			},function(){
				var layer_tip = layer.load(1,{shade: [0.5,'#000']});
				$.post(WY_url,{'id':field_data},function(ret_data){
					layer.close(layer_tip);
					if(ret_data.code == 1){
						layer.msg(ret_data.msg,{icon: 1},function(){
							if(WY_table){table.reload(WY_table);}
						});

					}else{
						layer.msg(ret_data.msg,{icon: 2});
					}
				},"JSON");
			});
		}else{
			var layer_tip = layer.load(1,{shade: [0.5,'#000']});
			$.post(WY_url,{'id':field_data},function(ret_data){
				layer.close(layer_tip);
				if(ret_data['code'] == 1){
					layer.msg(ret_data.msg,{icon: 1},function(){
						if(WY_table){table.reload(WY_table);}
					});
				}else{
					layer.msg(ret_data.msg,{icon: 2});
				}
			},"JSON");
		}
	}
	// ajax get 事件
	function fun_lay_ajax_get(WY_url,WY_tips,WY_table,WY_refresh){
		if(WY_tips){
			layer.confirm(WY_tips,{
				btn: ['确定','取消'] //按钮
			},function(){
				var layer_tip = layer.load(1,{shade: [0.5,'#000']});
				$.get(WY_url,function(ret_data){
					layer.close(layer_tip);
					if(ret_data.code == 1){
						layer.msg(ret_data.msg,{icon: 1},function(){
							if(WY_table){table.reload(WY_table);}
							if(WY_refresh == 1){
								window.location.reload();
							}
						});

					}else{
						layer.msg(ret_data.msg,{icon: 2});
					}
				},"JSON");
			});
		}else{
			var layer_tip = layer.load(1,{shade: [0.5,'#000']});
			$.get(WY_url,function(ret_data){
				layer.close(layer_tip);
				if(ret_data['code'] == 1){
					layer.msg(ret_data.msg,{icon: 1},function(){
						if(WY_table){table.reload(WY_table);}
					});
				}else{
					layer.msg(ret_data.msg,{icon: 2});
				}
			},"JSON");
		}
	}
	// tab 窗口
	function fun_lay_parent_tab(WY_url,WY_id,WY_title){
		if ($parent('iframe[id="tree_id_'+WY_id+'"]')[0]) {
			parent_element.tabDelete('WyTab', WY_id);
		}
		parent_tab.add(WY_title,WY_url,WY_id);
		parent_tab.change(WY_id);
	}
	function fun_lay_tab(WY_url,WY_id,WY_title){
		if ($('iframe[id="tree_id_'+WY_id+'"]')[0]) {
			element.tabDelete('WyTab', WY_id);
		}
		tab.add(WY_title,WY_url,WY_id);
		tab.change(WY_id);
	}
	function fun_lay_ifram(WY_url,WY_title){
		console.log(WY_url+':'+WY_title);
		
	}
	function fun_lay_parent_ifram(WY_url,WY_title){
		console.log(WY_url+':'+WY_title);
		parent_layer.open({
			type: 2,
			title: WY_title,
			shadeClose: true,
			shade: true,
			area: ['100%', '100%'],
			content: WY_url
		});
	}
	function fun_lay_top_ifram(WY_url,WY_title){
		console.log(WY_url+':'+WY_title);
		// top_layer.open({
		top_layer.open({
			type: 2,
			title: WY_title,
			shadeClose: true,
			shade: true,
			area: ['100%', '100%'],
			content: WY_url
		});
	}

	var parent_tab = {
		add: function(title, url, id){
			parent_element.tabAdd('WyTab',{
				title: '<i class="layui-icon layui-icon-refresh" lay-id="'+id+'"></i>'+title,
				content: '<iframe id="tree_id_'+id+'" width="100%" height="100%" lay-id="'+id+'" frameborder="0" src="'+url+'" scrolling="yes" class="hs-iframe"></iframe>',
				id: id
			});
		},
		del: function( id) {
			parent_element.tabDelete('WyTab', id);
		},
		change: function( id) {
			parent_element.tabChange('WyTab', id);
		}
	};

	/* 监听表单提交 */
	form.on('submit(wy-submit)',function(data){
		var _this = $(this);
		var _form_action = '';
		if(_this.attr('wy-form')) {
			_form_action= $(_this.attr('wy-form'));
		}else{
			_form_action = _this.parents('form');
		}
		var WY_changetabid = _this.attr('wy-changetabid')||'';
		var WY_closetabid = _this.attr('wy-closetabid')||'';
		var WY_reloadtable = _this.attr('wy-reloadtable')||'';
		var WY_reloadpage = _this.attr('wy-reloadpage')||'0';
		var layer_tip = layer.load(1,{shade: [0.5,'#000']});

		$.post(
			_form_action.attr('action'),
			_form_action.serialize(),
			function(ret_data){
				layer.close(layer_tip);
				if(ret_data['code'] == 1){
					if(WY_changetabid){
						parent_tab.change(WY_changetabid);
					}
					if(WY_reloadtable && WY_changetabid){
						var bdoy = $parent.find("iframe#tree_id_"+WY_changetabid)[0].contentWindow;
						bdoy.layui.table.reload(WY_reloadtable);
					}
					if(WY_changetabid && WY_reloadpage == 1){
						$parent.find("iframe#tree_id_"+WY_changetabid)[0].contentWindow.location.reload(true);
					}
					if(WY_closetabid){
						parent_tab.del(WY_closetabid);
					}
				}else{
					layer.msg(ret_data.msg,{icon: 2});
				}
			},
			'JSON'
		);
		return false;
	});
	form.verify({
		username: function(value, item){ //value：表单的值、item：表单的DOM对象
			if(value){
				if(!(/^[a-zA-Z0-9_]{4,18}$/.test(value))){
					return '只允许输入字母数字下划线，字符长度4-18位';
				}
				if(/(^\_)|(\__)|(\_+$)/.test(value)){
					return '首尾不能出现下划线\'_\'';
				}
				if(/^\d+\d+\d$/.test(value)){
					return '不能全为数字';
				}
			}
		},
		route: function(value, item){ //value：表单的值、item：表单的DOM对象
			if(value){

				if(value.indexOf("/") === false){

					if(!(/^[a-zA-Z0-9_-]{4,20}$/.test(value))){
						return '只允许输入字母、数字、下划线、中横线、斜线，字符长度4-20位';
					}
					if(/(^\_)|(\__)|(\_+$)/.test(value)){
						return '首尾不能出现下划线\'_\'';
					}
					if(/(^\-)|(\——)|(\-+$)/.test(value)){
						return '首尾不能出现中划线\'-\'';
					}
					if(/^\d+\d+\d$/.test(value)){
						return '不能全为数字';
					}

				}else{
					var route_arr = value.split("/");
					var tips = '';
					if(route_arr.length > 2){
						return '斜线 \'/\' 最多3个';
					}
					for(var i = 0;i < route_arr.length ; i++){
						if(!route_arr[i]){
							tips = '收尾不允许出现斜线 \'/\' 同事两个斜线之间不能相连';
							break;
						}
						if(!(/^[a-zA-Z0-9_-]{2,10}$/.test(route_arr[i]))){
							tips = ' \'/\'之间只允许输入字母、数字、下划线、中横线，字符长度2-10位';
							break;
						}

						if(/(^\_)|(\__)|(\_+$)/.test(route_arr[i])){
							return ' \'/\'之间首尾不能出现下划线\'_\'';
						}
						if(/(^\-)|(\——)|(\-+$)/.test(route_arr[i])){
							return ' \'/\'之间首尾不能出现中划线\'-\'';
						}
						if(/^\d+\d+\d$/.test(route_arr[i])){
							return ' \'/\'之间不能全为数字';
						}
					}
					if(tips!=''){
						return tips;
					}
				}
			}
		},
		pass: function(value, item){
			if(value){
				if(/s/.test(value)){
					return '不能出现空格';
				}
				if(value.length > 18 || value.length <6){
					return '密码必须6到18位';
				}
			}
		}
	});







	// 上传组件
	var uploadInst = [];
	function uploads_init(div_id){
		var wy_data = $("#"+div_id).attr('wy-data');
		var data_json = []
		if(wy_data){
			data_json = eval('(' + wy_data + ')');
		}
		var upload_config = [];
		upload_config['elem'] = '#'+div_id;
		upload_config['url'] = ajax_upload_url;
		upload_config['multiple'] = false;
		upload_config['field'] = 'file';

		if(data_json.accept){
			upload_config['accept'] = data_json.accept;
		}
		if(data_json.acceptMime){
			upload_config['acceptMime'] = data_json.acceptMime;
		}
		if(data_json.exts){
			upload_config['exts'] = data_json.exts;
		}
		if(data_json.data){
			upload_config['data'] = data_json.data;
		}
		if(data_json.size){
			upload_config['size'] = data_json.size;
		}else{
			upload_config['size'] = 0;
		}

		upload_config['choose'] = function(obj){
			//console.log('choose');
			//console.log(obj);
			layer.closeAll('loading');
		};

		//上传前
		upload_config['before'] = function(obj){
			layer.load();
			//console.log('before');
			//console.log(obj);
		};

		//上传完毕
		upload_config['done'] = function(res, index, upload){
			layer.closeAll('loading');
			// console.log('done');
			// console.log('res');
			// console.log(res);
			// console.log('index');
			// console.log(index);
			// console.log('upload');
			// console.log(upload);
			if(res.code == 1){
				if(data_json.input_url){
					$(data_json.input_url).val(res.data.url);
				}
				if(data_json.img_url){
					$(data_json.img_url).attr('src',res.data.url);
				}
			}else{
				layer.msg(res.msg);
			}
		};
		//上传失败
		upload_config['error'] = function(index, upload){
			layer.closeAll('loading');
			// console.log('error');
			// console.log('index');
			// console.log(index);
			// console.log('upload');
			// console.log(upload);
		};

		uploadInst[div_id] = upload.render(upload_config);
		return;
	}
	
	// 编辑器
	// case 'basics': // 基础
	// case 'simple': // 简单
	// case 'complete': // 完整
	/*
	$(".wy-type-basics").each(function(){
		
	});
	$(".wy-type-simple").each(function(){
		
	});
	$(".wy-type-complete").each(function(){
		
	});
	*/
	$(".wy-upload").each(function(){
		var div_id = $(this).attr("id");
		uploads_init(div_id);
	});
	$(document).on("click",".wy-work",function(){
		var div_id = $(this).attr("id");
		var wy_data = $("#"+div_id).attr('wy-data');
		var data_json = []
		if(wy_data){
			data_json = eval('(' + wy_data + ')');
		}
		
		var _url = layer_upload_url+'?';
		if(data_json.path){
			_url += 'path='+data_json.path+'&';
		}
		if(data_json.exts){
			_url += 'exts='+data_json.exts+'&';
		}
		if(data_json.img_url){
			_url += 'img_url='+data_json.img_url+'&';
		}
		if(data_json.input_url){
			_url += 'input_url='+data_json.input_url+'&';
		}
		layer.open({
			type: 2,
			title: '文件空间',
			area: ['750px', '500px'],
			content: _url, //iframe的url，no代表不显示滚动条
		});
	});
	$(document).on("click",".change_upload",function(){
		var wy_data = $(this).attr('wy-data');
		var wy_src = $(this).attr('wy-src');
		var data_json = [];
		if(wy_data){
			data_json = eval('(' + wy_data + ')');
		}
		
		
		if(data_json.input_url){
			$parent(data_json.input_url).val(wy_src);
		}
		if(data_json.img_url){
			$parent(data_json.img_url).attr('src',wy_src);
		}
		
		parent_layer.closeAll();
		
	});

	//日期时间范围
	$(".wy-datetime-range").each(function(){
		laydate.render({
			elem: this,
			type: 'datetime',
			range: '~',
		});
	});
	//日期
	$(".wy-date").each(function(){
		laydate.render({
			elem: this,
			type: 'datetime',
		});
	});
	//日期时间
	$(".wy-datetime").each(function(){
		laydate.render({
			elem: this,
			type: 'datetime',
		});
	});

	/**
	 * 轮播
	 * 快捷菜单
	**/
	$(".wy-carousel").each(function(){
		carousel.render({
			elem: $(this),
			autoplay: false,
			arrow: 'none',
			width: '100%',
			trigger: 'mouseover',
		});
	});
    exports('global', {});
});